"use strict"

/**
 * 长春市政府采购网爬虫
 *  
 */

var cheerio = require("cheerio");
var q = require("q");
var Base = require("./base");
var cc_spider = new Base();
cc_spider.name = "长春市政府采购网";
cc_spider.page = 1;
var in_spider;
var sj_pc_1 = function () {// 省局爬虫
        var d = q.defer();
        var url = "http://www.cczfcg.gov.cn/article/bid_list.action?field=1&d-16544-p=1&type=1";
        in_spider.encode = "utf-8";
        in_spider.get(url, function (data) {
            var $ = cheerio.load(data);
            $("#row a").each(function (index, ele) {
                var e = $(ele);
                var item = {};
                 item.title = e.text();
                 item.url = "http://www.cczfcg.gov.cn" + e.attr("href");
	             item.source = cc_spider.name;
	             cc_spider.items.push(item);
                
            });
            d.resolve();
        });
        return d.promise;
}
var sj_pc_2 = function () {// 省局爬虫
        var d = q.defer();
        var url = "http://www.cczfcg.gov.cn/article/bid_list.action?field=1&d-16544-p=2&type=1";
        in_spider.encode = "utf-8";
        in_spider.get(url, function (data) {
            var $ = cheerio.load(data);
            $("#row a").each(function (index, ele) {
                var e = $(ele);
                var item = {};
                 item.title = e.text();
                 item.url = "http://www.cczfcg.gov.cn" + e.attr("href");
	             item.source = cc_spider.name;
	             cc_spider.items.push(item);
                
            });
            d.resolve();
        });
        return d.promise;
}
var sj_pc_3 = function () {// 省局爬虫
        var d = q.defer();
        var url = "http://www.cczfcg.gov.cn/article/bid_list.action?field=1&d-16544-p=3&type=1";
        in_spider.encode = "utf-8";
        in_spider.get(url, function (data) {
            var $ = cheerio.load(data);
            $("#row a").each(function (index, ele) {
                var e = $(ele);
                var item = {};
                 item.title = e.text();
                 item.url = "http://www.cczfcg.gov.cn" + e.attr("href");
	             item.source = cc_spider.name;
	             cc_spider.items.push(item);
                
            });
            d.resolve();
        });
        return d.promise;
}
var xq_pc_1 = function () {// 县区爬虫
        var d = q.defer();
        var url = "http://www.cczfcg.gov.cn/article/bid_list.action?field=2&d-16544-p=1&type=1";
        in_spider.encode = "utf-8";
        in_spider.get(url, function (data) {
            var $ = cheerio.load(data);
            $("#row a").each(function (index, ele) {
                var e = $(ele);
                var item = {};
                 item.title = e.text();
                 item.url = "http://www.cczfcg.gov.cn" + e.attr("href");
	             item.source = cc_spider.name;
	             cc_spider.items.push(item);
                
            });
            d.resolve();
        });
        return d.promise;
}
var xq_pc_2 = function () {// 县区爬虫
        var d = q.defer();
        var url = "http://www.cczfcg.gov.cn/article/bid_list.action?field=2&d-16544-p=2&type=1";
        in_spider.encode = "utf-8";
        in_spider.get(url, function (data) {
            var $ = cheerio.load(data);
            $("#row a").each(function (index, ele) {
                var e = $(ele);
                var item = {};
                 item.title = e.text();
                 item.url = "http://www.cczfcg.gov.cn" + e.attr("href");
	             item.source = cc_spider.name;
	             cc_spider.items.push(item);
                
            });
            d.resolve();
        });
        return d.promise;
}
var xq_pc_3 = function () {// 县区爬虫
        var d = q.defer();
        var url = "http://www.cczfcg.gov.cn/article/bid_list.action?field=2&d-16544-p=3&type=1";
        in_spider.encode = "utf-8";
        in_spider.get(url, function (data) {
            var $ = cheerio.load(data);
            $("#row a").each(function (index, ele) {
                var e = $(ele);
                var item = {};
                 item.title = e.text();
                 item.url = "http://www.cczfcg.gov.cn" + e.attr("href");
	             item.source = cc_spider.name;
	             cc_spider.items.push(item);
                
            });
            d.resolve();
        });
        return d.promise;
}

 cc_spider.getItemWithPage = function (spider, pg) {
    in_spider = spider;
    return function () {
        var d = q.defer();
        var defer = q('');
        defer.then(sj_pc_1).then(sj_pc_2).then(sj_pc_3).then(xq_pc_1).then(xq_pc_2).then(xq_pc_3).then(function () {
            d.resolve();
        });
        return d.promise;
    };
}

exports = module.exports = cc_spider;
